@echo off
echo ========================================
echo RefundAuditWeb - IIS Deployment Package Tool
echo ========================================
echo.

:: Set variables
set SOURCE_DIR=%~dp0
set DEPLOY_DIR=%SOURCE_DIR%deploy
set TIMESTAMP=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
set TIMESTAMP=%TIMESTAMP: =0%
set ZIP_NAME=RefundAuditWeb_Deploy_%TIMESTAMP%.zip

echo Source Directory: %SOURCE_DIR%
echo Deploy Directory: %DEPLOY_DIR%
echo.

:: Create deploy directory
if exist "%DEPLOY_DIR%" (
    echo Cleaning old deployment files...
    rmdir /s /q "%DEPLOY_DIR%"
)
mkdir "%DEPLOY_DIR%"

echo Copying deployment files...
echo.

:: Copy necessary files
echo [1/6] Copying main page file...
copy "%SOURCE_DIR%index.html" "%DEPLOY_DIR%\" >nul
if errorlevel 1 (
    echo Error: Cannot copy index.html
    pause
    exit /b 1
)

echo [2/6] Copying JavaScript files...
copy "%SOURCE_DIR%api.production.js" "%DEPLOY_DIR%\api.js" >nul
copy "%SOURCE_DIR%app.js" "%DEPLOY_DIR%\" >nul
if errorlevel 1 (
    echo Error: Cannot copy JavaScript files
    pause
    exit /b 1
)
echo     Note: Using production API configuration

echo [3/6] Copying IIS configuration file...
copy "%SOURCE_DIR%web.config" "%DEPLOY_DIR%\" >nul
if errorlevel 1 (
    echo Error: Cannot copy web.config
    pause
    exit /b 1
)

echo [4/6] Copying static resource files...
if exist "%SOURCE_DIR%static" (
    xcopy "%SOURCE_DIR%static" "%DEPLOY_DIR%\static\" /e /i /q >nul
    if errorlevel 1 (
        echo Warning: Static files copy may be incomplete
    )
) else (
    echo Warning: Static folder not found
)

echo [5/6] Copying deployment documentation...
copy "%SOURCE_DIR%IIS部署说明.md" "%DEPLOY_DIR%\" >nul
if errorlevel 1 (
    echo Warning: Cannot copy deployment documentation
)

echo [6/6] Creating deployment info file...
echo RefundAuditWeb - IIS Deployment Package > "%DEPLOY_DIR%\DeploymentInfo.txt"
echo Package Time: %date% %time% >> "%DEPLOY_DIR%\DeploymentInfo.txt"
echo Port Configuration: 8089 >> "%DEPLOY_DIR%\DeploymentInfo.txt"
echo. >> "%DEPLOY_DIR%\DeploymentInfo.txt"
echo Deployment Steps: >> "%DEPLOY_DIR%\DeploymentInfo.txt"
echo 1. Copy all files to C:\inetpub\wwwroot\RefundAuditWeb\ >> "%DEPLOY_DIR%\DeploymentInfo.txt"
echo 2. Create website in IIS with port 8089 >> "%DEPLOY_DIR%\DeploymentInfo.txt"
echo 3. Follow detailed steps in IIS deployment guide >> "%DEPLOY_DIR%\DeploymentInfo.txt"

echo.
echo ========================================
echo Package Complete!
echo ========================================
echo.
echo Deployment files ready at: %DEPLOY_DIR%
echo.
echo Deployment file list:
dir "%DEPLOY_DIR%" /b
echo.
echo Next steps:
echo 1. Copy all files from deploy folder to server
echo 2. Configure IIS following the deployment guide
echo 3. Ensure backend API service is running
echo.
echo Open deployment folder? (Y/N)
set /p choice=Choose: 
if /i "%choice%"=="Y" (
    explorer "%DEPLOY_DIR%"
)

echo.
echo After deployment, visit: http://localhost:8089
echo Or: http://server-ip:8089
echo.
pause